package com.yjq.blog;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Slf4j
@EnableSwagger2
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class ArticleApplication {

    public static void main(String[] args) {
        log.info("ArticleApplication Start");
        ConfigurableApplicationContext context = SpringApplication.run(ArticleApplication.class, args);
        logSwaggerUrl(context);
    }

    private static void logSwaggerUrl(ConfigurableApplicationContext context) {
        try {
            MutablePropertySources propertySources = context.getEnvironment().getPropertySources();
            PropertySource<?> source = propertySources.get("applicationConfig: [classpath:/application.yml]");
            if (source != null) {
                Object port = source.getProperty("server.port");
                Object name = source.getProperty("server.servlet.context-path");
                log.info("http://localhost:{}/{}/swagger-ui.html#/", port, name);
            }
        } catch (Exception e) {
            log.error("打印swagger地址失败！！！");
        }
    }

}
